package com.lf.sys.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lf.sys.model.SysContact;
import com.lf.sys.service.ContactService;
import com.lf.sys.util.CommonUtils;
import com.lf.sys.util.LayuiBean;
import com.lf.sys.util.ResultBean;

@Controller
@RequestMapping("/contact")
public class ContactController {
	@Resource
	private ContactService contactService;
	private Logger logger = LoggerFactory.getLogger(getClass());

	private long getUserId() {
		return 1;
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, Model model) {
		return "mail/contact_add";
	}

	@RequestMapping("/addDo")
	@ResponseBody
	public String addDo(HttpServletRequest request, Model model) {
		Long userId = this.getUserId();
		String name = CommonUtils.getString(request.getParameter("name")).trim();
		String email = CommonUtils.getString(request.getParameter("email")).trim();
		if (StringUtils.isBlank(name) || StringUtils.isBlank(email)) {
			return JSON.toJSONString(new ResultBean(0, "请补充信息", null));
		}
		SysContact temp = new SysContact();
		temp.setEmail(email);
		temp.setName(name);
		temp.setUserId(userId);
		if (contactService.insert(temp) > 0) {
			return JSON.toJSONString(new ResultBean(1, "添加成功", null));
		} else {
			return JSON.toJSONString(new ResultBean(0, "添加失败", null));
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(HttpServletRequest request, Model model) {
		Long userId = this.getUserId();
		Long id;
		try {
			id = Long.parseLong(CommonUtils.getString(request.getParameter("id")).trim());
		} catch (Exception e) {
			return JSON.toJSONString(new ResultBean(0, "请选择联系人", null));
		}
		if (contactService.delete(id, userId) > 0) {
			return JSON.toJSONString(new ResultBean(1, "删除成功", null));
		} else {
			return JSON.toJSONString(new ResultBean(0, "删除失败", null));
		}
	}

	@RequestMapping("/update")
	public String update(HttpServletRequest request, Model model) {
		Long userId = this.getUserId();
		Long id;
		try {
			id = Long.parseLong(CommonUtils.getString(request.getParameter("id")).trim());
		} catch (Exception e) {
			model.addAttribute("msg", "请选择联系人");
			return "mail/error";
		}
		SysContact temp = contactService.get(id, userId);
		if (temp == null || temp.getId() < 1) {
			model.addAttribute("msg", "联系人不存在");
			return "mail/error";
		}
		model.addAttribute("contact", temp);
		return "mail/contact_update";
	}

	@RequestMapping("/updateDo")
	@ResponseBody
	public String updateDo(HttpServletRequest request, Model model) {
		Long userId = this.getUserId();
		Long id;
		String name = CommonUtils.getString(request.getParameter("name")).trim();
		String email = CommonUtils.getString(request.getParameter("email")).trim();
		try {
			id = Long.parseLong(CommonUtils.getString(request.getParameter("id")).trim());
		} catch (Exception e) {
			return JSON.toJSONString(new ResultBean(0, "请选择联系人", null));
		}
		if (StringUtils.isBlank(name) || StringUtils.isBlank(email)) {
			return JSON.toJSONString(new ResultBean(0, "请补充信息", null));
		}
		SysContact temp = new SysContact();
		temp.setEmail(email);
		temp.setId(id);
		temp.setName(name);
		temp.setUserId(userId);
		if (contactService.update(temp) > 0) {
			return JSON.toJSONString(new ResultBean(1, "修改成功", null));
		} else {
			return JSON.toJSONString(new ResultBean(0, "修改失败", null));
		}
	}

	@RequestMapping("/all")
	public String all(HttpServletRequest request, Model model) {
		return "mail/contact_all";
	}

	@RequestMapping("/page")
	@ResponseBody
	public String page(HttpServletRequest request, Model model) {
		long userId = this.getUserId();
		int page = 0;
		int limit = 10;

		try {
			page = Integer.parseInt(CommonUtils.getString(request.getParameter("page")));
		} catch (Exception e) {
			e.printStackTrace();
			page = 0;
		}
		if (page > 0) {
			page--;
		}
		try {
			limit = Integer.parseInt(CommonUtils.getString(request.getParameter("limit")));
		} catch (Exception e) {
			e.printStackTrace();
			limit = 10;
		}
		List<SysContact> result = contactService.listByPage(userId, page * limit, limit);
		int size = contactService.count(userId);
		LayuiBean<SysContact> layuiBean = new LayuiBean<SysContact>(0, "SUCC", size, result);
		return JSON.toJSONString(layuiBean);
	}

}
